package com.huluobo.a2201mvvm_imdemo.mvpdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.huluobo.a2201mvvm_imdemo.R;

public class LoginDemoActivity extends AppCompatActivity implements LoginView {

    LoginPresenter presenter = new LoginPresenter(this);
    public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{2,19}$";
    public static final String REGEX_PASSWORD = "^[0-9]{3,20}$";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_demo);
        EditText username = findViewById(R.id.login_demo_username);
        EditText password = findViewById(R.id.login_demo_password);
        Button loginBtn = findViewById(R.id.login_demo_btn);

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (username.getText().toString().matches(REGEX_USERNAME)){
                    presenter.login(username.getText().toString(), password.getText().toString());
                }else {
                    Toast.makeText(LoginDemoActivity.this, "用户名不匹配", Toast.LENGTH_SHORT).show();
                }
            }
        });


    }

    @Override
    public void success(UserBean userBean) {
        Toast.makeText(this, "登录成功:" + userBean.getUsername() + "," + userBean.getPassword(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void failed(String error) {
        Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
    }
}